//
//  Date.hpp
//  日期类的实现
//
//  Created by 卜绎皓 on 2022/9/20.
//

#ifndef Date_hpp
#define Date_hpp
#endif /* Date_hpp */

#pragma once
#include<iostream>
#include<assert.h>
#include<stdbool.h>
using namespace std;
class Date
{
    friend ostream& operator << (ostream& out , const Date& d);
    friend istream& operator >> (istream& in , Date& d);
public:
    //获取某年某月的天数
    int GetMonthDay(int year, int month)
    {
        assert(month > 0 && month < 13);
        //默认平年
        int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        //判断润年的二月
        if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
        {
            return 29;
        }
        return monthDays[month];
    }
    Date(int year = 0, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
        //判断日期是否合法
        if (_year < 0 || _month <= 0 || _month >= 13 || _day <= 0 || _day > GetMonthDay(_year, _month))
        {
            cout << _year << "/" << _month << "/" << _day << "->";
            cout << "非法日期" << endl;
        }
    }
    void Print() const
    {
        cout << _year << "/" << _month << "/" << _day << endl;
    }
    bool operator>(const Date& d) const
    {
        if (_year > d._year)
            return true;
        else if (_year == d._year && _month > d._month)
            return true;
        else if (_year == d._year && _month == d._month && _day > d._day)
            return true;
        else
            return false;
    }
    bool operator==(const Date& d) const
    {
        return _year == d._year && _month == d._month && _day == d._day;
    }
    bool operator>=(const Date& d) const
    {
        return *this > d || *this == d;//复用operator>、operator==
    }
    bool operator<(const Date& d) const
    {
        return !(*this >= d);//复用operator>=，再取反
    }
    bool operator<=(const Date& d) const
    {
        return !(*this > d);//复用operator>，再取反
    }
    bool operator!=(const Date& d) const
    {
        return !(*this == d);//复用operator==，再取反
    }
    
    Date operator+(int day) const;
    Date operator-(int day) const;
    Date& operator+=(int day);
    Date& operator-=(int day);
    Date& operator++();
    Date operator++(int);
    Date& operator--();
    Date operator--(int);
    int operator-(const Date& d) const;
private:
    int _year;
    int _month;
    int _day;
    
//    inline ostream& operator << (ostream& out , const Date& d)
//    {
//        out << d._year <<"年"<<d._month<<"月"<<d._day<<"年"<<endl;
//        return out;
//    }
//
//    inline istream& operator >> (istream& in, Date& d)
//    {
//        in >> d._year >> d._month >> d._day;
//        return in;
//    }
    
    
};

